<?php
/**
 * Created by PhpStorm.
 * User: lingyun
 * Date: 2018/1/24
 * Time: 17:33
 * Desc: 公共基础类
 */
namespace app\index\controller;
use think\Controller;
use think\Db;
use pinyin\Pinyin;
use controller\BasicWechat;
class Base extends Controller{
    /**
     * @Author  lingyun
     * @Desc    基础方法 -- 检测浏览器类型
     *          1、检测是否有历史选择的城市、门店，没有统一到首页进行选择门店
     *          2、微信坏境自动获取openid
     * 2018-03-29
     */
    public function _initialize(){
        //微信客户端 -- 获取会员的openid
        if(is_weixin()){    //微信端
            if(empty(session('openid'))) {
                $oauth = new BasicWechat();     //微信授权登录,获取openID
            }
        }
        /**
         * 根据ip定位当前的城市、获取保存的门店或者后台推荐的门店地址
         * 没有推荐门店或者推荐的门店已经关闭选择默认最早添加的店进行推荐
         */
        $cookie_city_key = cookie('city_key');                          //城市key -- 拼音
        $cookie_city_code = cookie('city_code');                        //城市主键表示 -- code
        $cookie_store_id = cookie('store_id');                          //上次选中的门店

        $ip = request()->ip();                                      //获取客户端的IP地址
        $result = get_location_city($ip);                           //返回当前的城市信息
        $pinyin = new Pinyin();                                     //将中文装换成拼音
        $city_key = $pinyin->getAllPY($result['msg']);              //当前城市的key
        $city = db('city')->where('key',$city_key)->find();         //当前的城市信息
        $city_list = db('city')->where(['status'=>1,'is_deleted'=>0])->select();                //切换城市的城市列表

        if($cookie_city_key == '' || $cookie_city_code == '' || $cookie_store_id == ''){            //没有历史记录，重新获取
            $this->assign('is_cookie_city',0);                          //没有选择城市或者门店
            $this->redirect('Index/index');
        }else{
            cookie('city_key',$cookie_city_key,15552000);               //保存当前的城市代码
            cookie('city_code',$cookie_city_code,15552000);             //城市的代码
            cookie('store_id',$cookie_store_id,15552000);               //城市的代码
            session('store_id',$cookie_store_id);                       //记住门店id
            $this->assign('is_cookie_city',1);                          //已经选择门店或者城市
        }
        $cur_city = db('city')->where('key',cookie('city_key'))->value('value');            //当前的城市名称

        $cart_num = $this->get_cart_amount();           //购物车商品数量

        $this->url = $this->request->url(true);         //登录回调页面
        $this->assign('url',$this->url);
        $this->assign('cur_city',$cur_city);            //当前的城市
        $this->assign('city_list',$city_list);            //当前城市名称
        $this->assign('cur_category',0);                //商品的顶级分类
        $this->assign('city_list',$city_list);               //城市列表
        $this->assign('cart_num',$cart_num);            //购物车数量
        $this->assign('wx_qrcode_img',sysconf('wechat_code_img'));
    }

    /**
     * @Author  lingyun
     * @Desc    空操作
     * @return string
     * 2018-03-29
     */
    public function _empty(){
        //把所有城市的操作解析到city方法
        return $this->fetch('public/404');
//        return '操作不存在';
    }

    /**
     * @Author  lingyun
     * @Desc    购物车数据
     * @return int
     * 2018-04-21
     */
    private function get_cart_amount(){
        $store_id = cookie('store_id');         //当前门店id
        $cart = (array)cookie('user_cart');            //会员购物车
        if(!empty($store_id)){                  //
            foreach($cart as $k => $v){
                if($v['store_id'] != $store_id){
                    unset($cart[$k]);
                }
            }
            $store_goods_stock_id = array_column($cart, 'store_goods_stock_id');
            $goods = db('store_goods_stock')->alias('a')->join('ys_goods b','a.goods_id=b.id')->join('ys_goods_spec_detail c','a.goods_spec_id=c.id','LEFT')->where(['a.is_deleted'=>0,'a.onsale'=>1,'c.is_deleted'=>0,'b.is_deleted'=>0,'status'=>1,'a.id'=>['in',$store_goods_stock_id]])->select();
            return count($goods);
        }else{
            return 0;
        }
    }

}
